<?php
function addCart($toadd)
{
	
	if($toadd != 'null')
	{
		$entry = explode("|", $toadd);
		/*
		[0] - id
		[1] - category: m/w/a
		[2] - quantity
		[3] - size
		[4] - session
		*/
		if($entry[2] > 0)
		{
			$tables = array();
			$tables[0] = 'ex_gallery_'.$entry[1];
			
			$fields = array();
			$fields[0] = 'itemID';
			$fields[1] = 'itemName';
			$fields[2] = 'itemPrice';
			$fields[3] = 'active';
			
			$whereClause = 'itemID="'.$entry[0].'"';
			
			$data = SQLRecover($tables, $fields, $whereClause);
			
			if($data['queryStatus'] == 'DB Query success!')
			{
				$track = true;
				$sizeOfSessh = (!isset($_SESSION['cart']) ? 0 : sizeOf($_SESSION['cart']));
				if($sizeOfSessh != 0)
				{
					for ($i=0; $i<$sizeOfSessh; $i++)
					{
						if($_SESSION['cart'][$i] == $toadd)
						{
							$track = false;
						}
					}
				}
				
				if($track)
				{
					$_SESSION['cart'][$sizeOfSessh] = $toadd;
					return '<script type="text/javascript">
				<!--
				alert(\'The item has been added to your shopping cart.\');
				-->
			</script>';
				}
			}
		}
	}
}
	function recoverProductDetails($id, $category)
	{
		$tableSearched = ($category == 'men' ? 'ex_gallery_m' : ($category == 'accessories' ? 'ex_gallery_a' : ($category == 'women' ? 'ex_gallery_w' : '')));
		
		$tables = array();
		$tables[0] = $tableSearched;
		
		$fields = array();
		$fields[0] = 'itemID';
		$fields[1] = 'itemName';
		$fields[2] = 'itemImg1';
		$fields[3] = 'itemImg2';
		$fields[4] = 'itemImg3';
		$fields[5] = 'itemPrice';
		$fields[6] = 'itemDesc';
		$fields[7] = 'active';
		$fields[8] = 'deactivate';
		if($category == 'men' || $category == 'women')
		{
			$fields[9] = 'size6';
			$fields[10] = 'size65';
			$fields[11] = 'size7';
			$fields[12] = 'size8';
			$fields[13] = 'size9';
			$fields[14] = 'size10';
		}
		if($category == 'men')
		{
			$fields[15] = 'size85';
			$fields[16] = 'size95';
			$fields[17] = 'size11';
		}
		else if($category == 'women')
		{
			$fields[15] = 'size85';
		}
		
		$whereClause = 'itemID = "'.$id.'"';
		
		$data = SQLRecover($tables, $fields, $whereClause);
		
		$return = array();
		
		$prices = number_format($data['itemPrice'][0], 2, '.', ',');
		
		if($data['active'][0] > 1)
		{
			$prices = '<del>PHP '.number_format($data['itemPrice'][0], 2, '.', ',').'</del> PHP '.number_format($data['itemPrice'][0] * ((100-$data['active'][0])/100), 2, '.', ',');
		}
		
		$return['itemName'] = $data['itemName'][0];
		$return['itemPrice'] = $prices;
		$return['size'] = '';
		$return['sizeText'] = '';
		$return['desc'] = $data['itemDesc'][0];
		$return['pic1'] = $data['itemImg1'][0];
		$return['pic2'] = $data['itemImg2'][0];
		$return['pic3'] = $data['itemImg3'][0];
		$return['sale'] = ($data['active'][0] > 1 ? true : false);
		$return['status'] = $data['queryStatus'];
		$return['sql'] = $data['traceSQL'];
		$return['maxQty'] = $data['deactivate'][0];
		
		if($category != 'accessories')
		{		
			$g = 0;
			$d = array();
			for ($i=9; $i<sizeOf($fields); $i++)
			{
				if($data[$fields[$i]][0] > 0)
				{
					$return['size'] .= '<option value="'.str_ireplace('size','', str_ireplace('5', '.5', $fields[$i])).'">'.str_ireplace('size','', str_ireplace('5', '.5', $fields[$i])).'</option>';
					
					$d['sizeAvail'][$g] = str_ireplace('size','', str_ireplace('5', '.5', $fields[$i]));
					$g++;
				}
			}
			
			for ($i=0; $i<sizeOf($d['sizeAvail']); $i++)
			{
				$o = sizeOf($d['sizeAvail'])-1;
				if($i == $o)
				{
					$return['sizeText'] .= $d['sizeAvail'][$i];
				}
				else
				{
					$return['sizeText'] .= $d['sizeAvail'][$i].', ';
				}
			}
		}
		return $return;
		
	}
	function displayPage()
	{
		$catImgFolder = './img/categories/'.get('category').'/'.get('sub').'/';
		$details = recoverProductDetails(get('id'), get('category'));
		$qtyOps = '';
		for ($i=0; $i<$details['maxQty']; $i++)
		{
			$j = $i + 1;
			$qtyOps .= '<option value="'.$j.'"'.($i == 0 ? 'selected' : '').'>'.$j.'</option>
			';
		}
		
		$output = '
		<div class="container_12">
			<div class="grid_12 crumbs">
				<a href="'.INDEX.'home">Home</a> > <a href="'.INDEX.'catalogue'.EXT.'category='.get('category').'&gallery=main">'.ucwords(get('category')).'</a> > <a href="'.INDEX.'catalogue'.EXT.'category='.get('category').'&gallery='.get('sub').'">'.matchSubNavName(get('sub')).'</a> > <a href="'.INDEX.'product'.EXT.'category='.get('category').'&sub='.get('sub').'&id='.get('id').'">'.ucwords($details['itemName']).'</a>
			</div>
		</div>
		<div class="container_16">
			<div class="grid_1">
				&nbsp;
			</div>
			<div class="grid_7 left side">
				<div class="imageContainer">
					'.($details['sale'] ? '<div class="sale"></div>' : '').'
					<img class="prodBig" id="pic1" src="'.$catImgFolder.$details['pic1'].'" />
					<img class="prodBig" id="pic2" src="'.$catImgFolder.$details['pic2'].'"'.($details['pic2'] == 'none' ? 'style="display: none;"' : '').' />
					<img class="prodBig" id="pic3" src="'.$catImgFolder.$details['pic3'].'"'.($details['pic3'] == 'none' ? 'style="display: none;"' : '').' />
				</div>
				<div class="smallImgs">
					<img class="smallPix" id="sm1" src="'.$catImgFolder.$details['pic1'].'" />
					<img class="smallPix" id="sm2" src="'.$catImgFolder.$details['pic2'].'"'.($details['pic2'] == 'none' ? 'style="display: none;"' : '').' />
					<img class="smallPix lastSmallPix" id="sm3" src="'.$catImgFolder.$details['pic3'].'"'.($details['pic2'] == 'none' ? 'style="display: none;"' : '').' />
				</div>
			</div>
			<div class="grid_7 right side">
				<h2>'.$details['itemName'].'</h2>
				<div class="price">
					<h4>Price: '.$details['itemPrice'].'</h4>
				</div>
				'.(get('category') == 'accessories' ? '' : '<div class="sizes">
					Sizes Available: <br />'.$details['sizeText'].'
				</div>').'
				<div class="description">
					'.$details['desc'].'
				</div>
				<h3>Purchase</h3>
				<table>
					'.(get('category') != 'accessories' ? '<tr>
						<td class="leftTable">
							Size:
						</td>
						<td>
							<select id="sizeOption" class="centerAlign">
								'.$details['size'].'
							</select>
						</td>
					</tr>' : '<input type="hidden" id="sizeOption" value="N/A" />').'
					<tr>
						<td class="leftTable">
							Quantity:
						</td>
						<td class="rightTable">
							<select id="qty" class="centerAlign">
								'.$qtyOps.'
							</select>
						</td>
					</tr>
					<tr>
						<td colspan="2">
							'.adminContent('', '<input type="button" id="addCart" value="Add To My Bag" '.(isset($_SESSION['shopper']) ? 'rel="stay"' : 'rel="cart"').'/>').'
						</td>
					</tr>
					<input type="hidden" id="goCart" value="'.INDEX.'cart'.EXT.'addCart="/>
					<input type="hidden" id="stayPage" value="'.INDEX.'product'.EXT.'category='.get('category').'&sub='.get('sub').'&id='.get('id').'&addCart="/>
					<input type="hidden" id="itemTable" value="'.substr(get('category'), 0, 1).'" />
					<input type="hidden" id="itemID" value="'.get('id').'"/>
					<input type="hidden" id="rand" value="'.rand(1, 1200).'"/>
				</table>
			</div>
			<div class="grid_1">
				&nbsp;
			</div>
		</div>
		';
		
		if(session('shopper') != 'null')
		{
			$output .= addCart(get('addCart'));
		}
		return $output;
	}
?>